package com.collect.senegal.zhiScene.zhiDs.zhiFollow;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.collect.senegal.R;
import com.collect.senegal.zhiAdater.zhiDs.ZHIDsListAdjuster;
import com.collect.senegal.zhiBase.BaseFragment;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRDsListEntity;
import com.collect.senegal.zhiScene.zhiDs.zhiRoot.ZHIRootDsFragmentInterface;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIDsFollowFragment_new extends BaseFragment<ZHIDsFollowPresenter> implements ZHIDsFollowContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.rc_list)
    RecyclerView rc_list;

    private ZHIRootDsFragmentInterface mZHIRootDsFragmentInterface;

    private final ZHIDsListAdjuster mZHIDsListAdjuster = new ZHIDsListAdjuster();

    @Override
    protected void initInjector() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_follow_ds;
    }

    @Override
    protected void initView(View view) {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));
            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mZHIRootDsFragmentInterface = (ZHIRootDsFragmentInterface) activity;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onGetLatestDsList(ZHIRDsListEntity zhirDsListEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            mZHIDsListAdjuster.setNewData(zhirDsListEntity.getList());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onMarkDsOrder() {
        initData(true);
    }

    @SuppressLint("InflateParams")
    public void initEvent() {
//        try {
//            srl_swipe_layout.setOnRefreshListener(() -> initData(false));
//            mZHIDsListAdjuster.setEmptyView(getRefreshEmptyView());
//            rc_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
//            rc_list.setAdapter(mZHIDsListAdjuster);
//
//            mZHIDsListAdjuster.setOnDsItemClickListener((zhirDsEntity, actionType) -> {
//                if (ZHIDsActionType.CALL_PHONE.equalsIgnoreCase(actionType)) {
//                    //电话操作
//                    ZHISysShareHelper.callPhone(getActivity(), zhirDsEntity.getContactMobile(), false);
//
//                } else if (ZHIDsActionType.CALL_WHATSAPP.equalsIgnoreCase(actionType)) {
//                    //whatsapp操作
//                    ZHISysShareHelper.sendMsgWithWhatsApp(getActivity(), ZHIProjectConfigs.PRE_PHONE + zhirDsEntity.getContactMobile(), "");
//
//                } else if (ZHIDsActionType.RESULT_NO_ANSWER.equalsIgnoreCase(actionType)) {
//                    //标记操作
//                    if (mPresenter != null) {
//                        ZHISDsMarkedOrder zhisDsMarkedOrder = new ZHISDsMarkedOrder(zhirDsEntity.getOrderId(), ZHIDsActionType.RESULT_NO_ANSWER, ZHIDsActionType.REMARK_NO_ANSWER, zhirDsEntity.getContactMobile());
//                        mPresenter.markDsOrder(getActivity(), zhisDsMarkedOrder, true);
//                    }
//
//                } else if (ZHIDsActionType.RESULT_KNOW_USER.equalsIgnoreCase(actionType)) {
//                    //标记操作
//                    if (mPresenter != null) {
//                        ZHISDsMarkedOrder zhisDsMarkedOrder = new ZHISDsMarkedOrder(zhirDsEntity.getOrderId(), ZHIDsActionType.RESULT_KNOW_USER, ZHIDsActionType.REMARK_KNOW_USER, zhirDsEntity.getContactMobile());
//                        mPresenter.markDsOrder(getActivity(), zhisDsMarkedOrder, true);
//                    }
//
//                } else if (ZHIDsActionType.RESULT_NOT_KNOW_USER.equalsIgnoreCase(actionType)) {
//                    //标记操作
//                    if (mPresenter != null) {
//                        ZHISDsMarkedOrder zhisDsMarkedOrder = new ZHISDsMarkedOrder(zhirDsEntity.getOrderId(), ZHIDsActionType.RESULT_NOT_KNOW_USER, ZHIDsActionType.REMARK_NOT_KNOW_USER, zhirDsEntity.getContactMobile());
//                        mPresenter.markDsOrder(getActivity(), zhisDsMarkedOrder, true);
//                    }
//                }
//            });
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    private void initData(boolean showLoading) {
        if (mPresenter != null) {
            mPresenter.getLatestDsList(getActivity(), showLoading);
        }
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> {
            initData(true);
        });
        return emptyView;
    }

    public void refreshData() {
        initData(true);
    }

}